package com.lanhai.common;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

@SuppressWarnings("unchecked")
public class BaseDaoHibernate extends HibernateDaoSupport implements IBaseDAO {
	
	protected Logger logger = Logger.getLogger(BaseDaoHibernate.class);

	public void deleteObject(Object o) {
		if(o == null) {
			logger.info("要删除掉对象不能为空");
			return ;
		}
		getHibernateTemplate().delete(o);
	}

	public Object getObject(Class clazz, Serializable id) {
		if(id == null) {
			logger.info("要获取的对象ID不能为空");
			return null;
		}
		Object o = getHibernateTemplate().get(clazz, id);
		return o;
	}

	public List getObjects(Class clazz) {
		return getHibernateTemplate().loadAll(clazz);
	}

	public void removeObject(Class clazz, Serializable id) {
		if(id == null) {
			logger.info("要删除的对象ID不能为空");
			return ;
		}
		getHibernateTemplate().delete(getObject(clazz, id));
	}

	public void saveObject(Object o) {
		getHibernateTemplate().save(o);
		getHibernateTemplate().flush();
	}

	public void saveOrUpdate(Object o) {
		getHibernateTemplate().merge(o);
		getHibernateTemplate().flush();
	}

	public void updateObject(Object o) {
		getHibernateTemplate().update(o);
		getHibernateTemplate().flush();
	}

}
